clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\cand_level_persist_rep.dta",clear

	
	texdoc close 
	cap erase "$dir/Tables/TableA2.tex"
	texdoc init "$dir/Tables/TableA2.tex", force

	tex \begin{table}[tbph]
	tex \caption{Running in next mayoral election (runner up and third-place candidate)}\label{tab:second_third}
	tex \centering
	tex \begin{tabular}{l c } \hline
	tex Outcome& Running again  \\
	tex & (1)  \\ \hline
	tex &  \\
	
	*Model 1
	foreach x in runs{
	
		*Summary statistics for the mean
		quietly sum `x' if  vote_margin2_3!=.
			local mean_`x' : di %5.3f r(mean)
			local sd_`x' : di %5.3f r(sd) 
		
		*Regressions
		rdrobust `x' vote_margin2_3,  p(1) vce(cluster muni_code)
		
		*Local's for the table
		local bw_`x' : di %5.2f `e(h_l)'
		local ser_`x' = round(`e(se_tau_rb)',0.001)
		local Neff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local N_`x' = `e(N)'
		local poly_`x' = `e(p)'
		local beta1_`x' : di %5.3f `e(tau_cl)'
		local beta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local ser1_`x' : di %5.3f `e(ci_l_rb)'
			local ser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local em1_`x' = (`beta1_`x''/`mean_`x'')*100 
			local em1_`x' : di %5.2f `em1_`x''
			
		*P-values
		local pval2_`x' : di %5.3f `e(pv_rb)'
		scalar pval2_`x' = e(pv_rb)
			

	}	
	


	*Continue table
	tex Second & `beta1_runs'  \\
	tex \ \ \ \ Robust p-value & `pval2_runs'  \\
	tex \ \ \ \ CI 95\%  & [`ser1_runs',`ser2_runs'] \\
	tex & \\
	
	tex Observations & `N_runs'  \\
	tex Bandwidth obs. & `Neff_runs'  \\
	tex Mean & `mean_runs' \\
	tex Bandwidth & `bw_runs' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. Robust p-values with clustering at the municipality level 95\% robust confidence intervals are computed following \cite{calonico_robust_2014}. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
